const express = require('express');
const comment = express.Router();
const { Comment } = require('../model/comment');

comment.get('/', async (req, res) => {
    let query = req.query.query
    let pagesize = parseInt(req.query.pagesize)
    let pagenum = parseInt(req.query.pagenum)
    let start = (pagenum - 1) * pagesize;
    let reg = new RegExp('.*' + query + '.*$')
    let comments = await Comment.find({ "content": { $regex: reg, $options: 'i' } }).limit(pagesize).skip(start).populate('uid').populate('aid');
    if (!comments) {
        return res.sendResult(null, 400, "没有搜索到评论");
    } else {
        let count = await Comment.find({ "content": { $regex: reg, $options: 'i' } }).countDocuments();
        let data = {};
        data.comments = comments;
        data.total = count;
        return res.sendResult(data, 200, "获取成功");
    }
})
//删除评论
comment.delete("/:id", async (req, res) => {
    await Comment.findOneAndDelete({ _id: req.params.id });
    res.sendResult(null, 200, "删除成功");
})

module.exports = comment;